package com.cjz.tool.qr.utils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import com.cjz.tool.qr.config.Setting;
import com.cjz.tool.qr.pojo.ParseResult;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

public class Util {

    public static BufferedImage captureScreen() {
        try {
            Robot robot = new Robot();
            return robot.createScreenCapture(new Rectangle(Setting.screenSize));

        } catch (AWTException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ParseResult parseImage(BufferedImage image) {
        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");

        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        QRCodeReader reader = new QRCodeReader();
        try {
            Result res = reader.decode(bitmap, hints);
            String resStr = res.getText();
            return new ParseResult(resStr != null && resStr.length() > 0, resStr);
        } catch (NotFoundException e) {
            e.printStackTrace();
            return new ParseResult(false, "没有找到QR码!");
        } catch (ChecksumException e) {
            e.printStackTrace();
            return new ParseResult(false, "校验和错误!");
        } catch (FormatException e) {
            e.printStackTrace();
            return new ParseResult(false, "格式错误!");
        }
    }

    public static void copyText2Clipboard(String writeMe) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(writeMe);
        clip.setContents(tText, null);
    }

    public static BufferedImage getImageFromClipboard() {

        try {


            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

            // 尝试直接读取图像数据
            if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
                Image image = (Image) clipboard.getData(DataFlavor.imageFlavor);

                // 直接处理图像转换
                if (image instanceof BufferedImage) {
                    return (BufferedImage) image;
                }

                // 强制加载图像
                MediaTracker tracker = new MediaTracker(new Panel());
                tracker.addImage(image, 0);
                tracker.waitForAll();

                int width = image.getWidth(null);
                int height = image.getHeight(null);
                if (width > 0 && height > 0) {
                    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g = bufferedImage.createGraphics();
                    g.drawImage(image, 0, 0, null);
                    g.dispose();
                    return bufferedImage;
                }
            }
        } catch (Exception ignored) {
        }
        return null;
    }
}